package com.csjbot.weedle_auto.adapter

import android.app.Activity
import android.content.Context
import android.text.method.ScrollingMovementMethod
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import com.csjbot.weedle_auto.R

/**
 * author : chenqi.
 * e_mail : 1650699704@163.com.
 * create_time : 8/21/17.
 */

class MainAdapter(val mContext: Context, private val acy: Activity) : BaseAdapter() {
    private val da: Array<String> = mContext.resources.getStringArray(R.array.test_item)

    private val context = mContext

    override fun getCount(): Int {
        return da.size
    }

    override fun getItem(position: Int): Any {
        return da[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var view: View
        val viewHolder: ViewHandler
        if (convertView == null) {
            view = View.inflate(context, R.layout.main_item, null)
            viewHolder = ViewHandler()
            view.tag = viewHolder
            viewHolder.title = view.findViewById(R.id.one_item_main) as TextView
            viewHolder.body = view.findViewById(R.id.two_item_main) as TextView
            viewHolder.bottom = view.findViewById(R.id.three_item_main) as TextView
            val dm = DisplayMetrics()
            acy.windowManager.defaultDisplay.getMetrics(dm)
            val heightPixels = dm.heightPixels
            val wight = dm.widthPixels
            val texth = (heightPixels * 0.18).toInt()
            viewHolder.body!!.height = texth
            val width = (wight * 0.2).toInt()
            viewHolder.title!!.width = width
            viewHolder.bottom!!.width = width
            viewHolder.bottom!!.setBackgroundResource(R.color.red)
        } else {
            view = convertView
            viewHolder = view.tag as ViewHandler
        }

        viewHolder.title!!.text = da[position]
        viewHolder.body!!.hint = "没有数据！"
        viewHolder.bottom!!.text = "不通过！"
        viewHolder.body!!.movementMethod = ScrollingMovementMethod.getInstance()
        return view
    }


    inner class ViewHandler {
        internal var title: TextView? = null
        internal var body: TextView? = null
        internal var bottom: TextView? = null
    }

    fun updata(position: Int, listView: ListView, data: Any, bottom: Boolean) {
        val firstvisible = listView.firstVisiblePosition
        val lastvisibale = listView.lastVisiblePosition
        if (position in firstvisible..lastvisibale) {
            //            if (position != 2) {
            val view = listView.getChildAt(position - firstvisible)
            val viewHolder = view.tag as ViewHandler
            //然后使用viewholder去更新需要更新的view。
            viewHolder.body!!.append(data.toString())
            if (bottom) {
                viewHolder.bottom!!.text = "通过！"
                viewHolder.bottom!!.setBackgroundResource(R.color.green)
            } else {
                viewHolder.bottom!!.text = "不通过！"
                viewHolder.bottom!!.setBackgroundResource(R.color.red)
            }
            val offset = viewHolder.body!!.lineCount * viewHolder.body!!.lineHeight
            if (offset > viewHolder.body!!.height) {
                viewHolder.body!!.scrollTo(0, offset - viewHolder.body!!.height)
            }
        }
    }

    fun changeStatus(textView: TextView, l: Boolean) {
        if (l) {
            textView.text = "通过！"
            textView.setBackgroundResource(R.color.green)
        } else {
            textView.text = "不通过！"
            textView.setBackgroundResource(R.color.red)
        }
    }

    fun bottomFromData(textView: TextView) {
        val offset = textView.lineCount * textView.lineHeight
        if (offset > textView.height) {
            textView.scrollTo(0, offset - textView.height)
        }
    }


    fun updata(listView: ListView) {
        val firstvisible = listView.firstVisiblePosition
        val lastvisibale = listView.lastVisiblePosition
        for (i in 0 until count) {
            if (i in firstvisible..lastvisibale) {
                val view = listView.getChildAt(i - firstvisible)
                val viewHolder = view.tag as ViewHandler
                //然后使用viewholder去更新需要更新的view。
                viewHolder.body!!.text = ""
                viewHolder.body!!.hint = "没有数据！"
                viewHolder.bottom!!.text = "不通过！"
                viewHolder.bottom!!.setBackgroundResource(R.color.red)
            }
        }
    }
}
